home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / stream.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  3.4 KB  |  147 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    stream.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include "stream.h"
  35.  
  36. #include "error.h"
  37. #include "prim.h"
  38.  
  39. /* primitives */
  40.  
  41. static Object eof_object_p (Object obj);
  42. static Object standard_input (void);
  43. static Object standard_output (void);
  44. static Object standard_error (void);
  45.  
  46. static struct primitive stream_prims[] =
  47. {
  48.     {"%open-input-file", prim_1, open_input_file},
  49.     {"%open-output-file", prim_1, open_output_file},
  50.     {"%close-stream", prim_1, close_stream},
  51.     {"%eof-object?", prim_1, eof_object_p},
  52.     {"%standard-input", prim_0, standard_input},
  53.     {"%standard-output", prim_0, standard_output},
  54.     {"%standard-error", prim_0, standard_error},
  55. };
  56.  
  57. /* globals */
  58. Object standard_input_stream;
  59. Object standard_output_stream;
  60. Object standard_error_stream;
  61.  
  62. void
  63. init_stream_prims (void)
  64. {
  65.     int num;
  66.  
  67.     num = sizeof (stream_prims) / sizeof (struct primitive);
  68.  
  69.     init_prims (num, stream_prims);
  70. }
  71.  
  72. Object
  73. make_stream (enum streamtype type, FILE * fp)
  74. {
  75.     Object obj;
  76.  
  77.     obj = allocate_object (sizeof (struct stream));
  78.  
  79.     STREAMTYPE (obj) = Stream;
  80.     STREAMSTYPE (obj) = type;
  81.     STREAMFP (obj) = fp;
  82.     return (obj);
  83. }
  84.  
  85. Object
  86. open_input_file (Object filename)
  87. {
  88.     FILE *fp;
  89.     char *str;
  90.  
  91.     str = BYTESTRVAL (filename);
  92.     fp = fopen (str, "r");
  93.     if (!fp) {
  94.     error ("open-input-file: could not open file", filename, NULL);
  95.     }
  96.     return (make_stream (Input, fp));
  97. }
  98.  
  99. Object
  100. open_output_file (Object filename)
  101. {
  102.     FILE *fp;
  103.     char *str;
  104.  
  105.     str = BYTESTRVAL (filename);
  106.     fp = fopen (str, "w");
  107.     if (!fp) {
  108.     error ("open-output-file: could not open file", filename, NULL);
  109.     }
  110.     return (make_stream (Output, fp));
  111. }
  112.  
  113. Object
  114. close_stream (Object stream)
  115. {
  116.     fclose (STREAMFP (stream));
  117.     return (unspecified_object);
  118. }
  119.  
  120. static Object
  121. eof_object_p (Object obj)
  122. {
  123.     if (obj == eof_object) {
  124.     return (true_object);
  125.     } else {
  126.     return (false_object);
  127.     }
  128. }
  129.  
  130. static Object
  131. standard_input (void)
  132. {
  133.     return (standard_input_stream);
  134. }
  135.  
  136. static Object
  137. standard_output (void)
  138. {
  139.     return (standard_output_stream);
  140. }
  141.  
  142. static Object
  143. standard_error (void)
  144. {
  145.     return (standard_error_stream);
  146. }
  147.